package game.engine;

import game.state.Play;

import java.util.*;

import org.newdawn.slick.*;
import org.newdawn.slick.geom.Vector2f;

public class UserControlledCharacter extends Entity {

	public static final String STAND_SOUTH_ANIM = "standSouthAnimation";
	public static final String RUN_SOUTH_ANIM = "runSouthAnimation";
	public static final String STAND_WEST_ANIM = "standWestAnimation";
	public static final String RUN_WEST_ANIM = "runWestAnimation";
	public static final String STAND_NORTH_ANIM = "standNorthAnimation";
	public static final String RUN_NORTH_ANIM = "runNorthAnimation";
	public static final String STAND_EAST_ANIM = "standEastAnimation";
	public static final String RUN_EAST_ANIM = "runEastAnimation";

	Map<String, Animation> animMap = new HashMap<String, Animation>();

	public UserControlledCharacter(String name, String gfxDir) {
		this.name = name;
		if (!gfxDir.endsWith("/")) {
			gfxDir += "/";
		}
		graphicsDir = gfxDir;
	}

	public void init() {

		x = 0;
		y = 0;

		// Build animations
		try {
			int[] durations = { 300, 300 };
			Image[] standLeft = { new Image(graphicsDir + FrameConstants.STAND_WEST_1, GameConstants.TRANSPARENT) };
			animMap.put(UserControlledCharacter.STAND_WEST_ANIM, new Animation(standLeft, durations[0], false));
			//			Image[] runLeft = { new Image(graphicsDir + FrameConstants.RUN_WEST_1, GameConstants.TRANSPARENT),
			//					new Image(graphicsDir + FrameConstants.RUN_WEST_2, GameConstants.TRANSPARENT) };
			//			animMap.put(UserControlledCharacter.RUN_WEST_ANIM, new Animation(runLeft, GameConstants.FRAME_DURATION, false));
			//
			//			Image[] standRight = { new Image(graphicsDir + FrameConstants.STAND_EAST_1, GameConstants.TRANSPARENT) };
			//			animMap.put(UserControlledCharacter.STAND_EAST_ANIM, new Animation(standRight, durations[0], false));
			//			Image[] runRight = { new Image(graphicsDir + FrameConstants.RUN_EAST_1, GameConstants.TRANSPARENT),
			//					new Image(graphicsDir + FrameConstants.RUN_EAST_2, GameConstants.TRANSPARENT) };
			//			animMap.put(UserControlledCharacter.RUN_EAST_ANIM, new Animation(runRight, GameConstants.FRAME_DURATION, false));
		} catch (SlickException e) {
			System.err.println("Images for Hero: '" + name + "' were not found in '" + graphicsDir + "'.");
			System.exit(-1);
		}

		currentSprite = animMap.get(UserControlledCharacter.STAND_WEST_ANIM);

	}

	@Override
	public void update() {

		Input input = Play.getInput();

		// Left-Right movement
		if (input.isKeyDown(ControlUtilities.KEY_LEFT)) {
			moveVector = new Vector2f(-1.0f, 0.0f);
		}
		else if (input.isKeyDown(ControlUtilities.KEY_RIGHT)) {
			moveVector = new Vector2f(1.0f, 0.0f);
		}
		else if (input.isKeyDown(ControlUtilities.KEY_UP)) {
			moveVector = new Vector2f(0.0f, -1.0f);
		}
		else if (input.isKeyDown(ControlUtilities.KEY_DOWN)) {
			moveVector = new Vector2f(0.0f, 1.0f);
		}
		else {
			moveVector = new Vector2f(0.0f, 0.0f);
		}

		// For testing
		if (input.isKeyPressed(Input.KEY_O)) {
			System.out.println("Player: " + this.name);
			System.out.println("\t(x, y): (" + this.x + ", " + this.y + ")");
			System.out.println("\tTile Coords: (" + ((int) this.x / 32) * 32 + ", " + ((int) this.y / 32) * 32 + ")");
		}

		if (input.isKeyPressed(Input.KEY_I)) {
			//things happen
		}

	}

	public Vector2f getMoveVector() {
		return moveVector;
	}

	public void setMoveVector(Vector2f moveVector) {
		this.moveVector = moveVector;
	}

}
